//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.TridentWordAddIn.Common;

namespace Microsoft.TridentWordAddIn.Presentation
{
    internal partial class SqlAccountInformation : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SqlAccountInformation"/> class.
        /// </summary>
        public SqlAccountInformation()
        {
            InitializeComponent();
            this.ctrUserNamePassword.OnValidation += new EventHandler<ConnectionValidateEventArgs>(ctrUserNamePassword_OnValidation);
        }

        /// <summary>
        /// Occurs when [on validation].
        /// </summary>
        public event EventHandler<ConnectionValidateEventArgs> OnValidation;

        #region Properties

        /// <summary>
        /// Gets or sets a value indicating whether this instance is trusted.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is trusted; otherwise, <c>false</c>.
        /// </value>
        public bool IsTrusted
        {
            get
            {
                return this.rbtnTrusted.Checked;
            }
            set
            {
                this.rbtnTrusted.Checked = value;
                this.rbtnAuthRequired.Checked = !value;
            }
        }

        /// <summary>
        /// Gets or sets the name of the server.
        /// </summary>
        /// <value>The name of the server.</value>
        public string ServerName
        {
            get
            {
                if (this.txtServerName.Text.Length == 0)
                {
                    return "-1";
                }

                return this.txtServerName.Text;
            }
            set
            {
                this.txtServerName.Text = value;
            }
        }

        /// <summary>
        /// Gets or sets the name of the user.
        /// </summary>
        /// <value>The name of the user.</value>
        public string UserName
        {
            get
            {
                return this.ctrUserNamePassword.UserName;
            }
            set
            {
                this.ctrUserNamePassword.UserName = value;
            }
        }

        /// <summary>
        /// Gets or sets the password.
        /// </summary>
        /// <value>The password.</value>
        public string Password
        {
            get
            {
                return this.ctrUserNamePassword.Password;
            }
            set
            {
                this.ctrUserNamePassword.Password = value;
            }
        }

        #endregion

        /// <summary>
        /// Determines whether this instance is valid.
        /// </summary>
        /// <returns></returns>
        public ValidationResult IsValid()
        {
            ValidationResult result = this.IsServerNameValid();
            if (!result.IsValid)
            {
                this.txtServerName_Validating(txtServerName, null);
            }

            if (!this.rbtnTrusted.Checked)
            {
                result.Merge(this.ctrUserNamePassword.IsValid());
            }

            return result;
        }

        /// <summary>
        /// Determines whether [is server name valid].
        /// </summary>
        /// <returns></returns>
        private ValidationResult IsServerNameValid()
        {
            ValidationResult result = new ValidationResult(!this.txtServerName.Text.Trim().IsNullOrEmpty());            
            if (!result.IsValid)
            {
                result.Messages.Add("Server name is required");
            }

            return result;
        }

        #region Events

        /// <summary>
        /// Handles the CheckedChanged event of the rbtnTrusted control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void rbtnTrusted_CheckedChanged(object sender, EventArgs e)
        {
            this.ctrUserNamePassword.Enabled = !this.rbtnTrusted.Checked;
            if (this.rbtnTrusted.Checked)
            {
                this.ctrUserNamePassword.ClearErrorProviders();
            }
        }

        /// <summary>
        /// Handles the Validating event of the txtServerName control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        private void txtServerName_Validating(object sender, CancelEventArgs e)
        {
            if (this.OnValidation != null)
            {
                ValidationResult result = this.IsServerNameValid();
                ConnectionValidateEventArgs conFormEventArgs = new ConnectionValidateEventArgs(result.GetMessage(), result.IsValid, "TextBox");
                this.OnValidation(this.txtServerName, conFormEventArgs);
            }
        }

        /// <summary>
        /// Handles the OnValidation event of the ctrUserNamePassword control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Microsoft.TridentWordAddIn.Common.ConnectionValidateEventArgs"/> instance containing the event data.</param>
        private void ctrUserNamePassword_OnValidation(object sender, ConnectionValidateEventArgs e)
        {
            this.OnValidation(sender, e);
        }

        #endregion
    }
}
